<?php

namespace Common\Model;

class BookModel extends XuecheModel {

    protected $pk = 'book_id';
    protected $tableName = 'book';
    protected $fields = array(
            'book_id', 'book_name','author_id','cate_id','introduction','img',
            'create_time','disabled', 'status','year','month','day','unique','tj_num','brow_num'
        );

    /**
     * 清空数据缓存
     *
     */
    public function ClearAllCache() {

    }

    //缓存设置
    public $_useCache = true;

    public function Validate($data) {
        if(empty($data['author_id']) || empty($data['book_name']) || empty($data['create_time']) || empty($data['cate_id']) ){
            return FALSE;
        }
        return true;
    }

    public function getBookById($book_id){
        if($book_id > 0){
            return $this->where("`{$this->pk}` = '{$book_id}'")->find();
        }
        return FALSE;
    }


    public function checkBookRepeart($unique){
        $id = $this->where("`unique` = '{$unique}'")->field($this->pk)->find();
        if($id[$this->pk] > 0){
            return $id[$this->pk];
        }
        return FALSE;
    }


    public function saveBook($data){
        if(!$this->Validate($data))            return FALSE;
        if($data[$this->pk] > 0){
            return $this->save($data);
        }else{
            return $this->add($data);
        }

    }

    public function getList($where = '', $order = '', $offset = 0, $limit = 18, $fields = '',$total = FALSE) {
        $cacheName = md5(__METHOD__.$where.$order.$offset.$fields);
        $cacheValue = S($cacheName);
        if($this->_useCache && $cacheValue){
            return $cacheValue;
        }
        $data = parent::getList($where, $order, $offset, $limit, $fields,$total);
        if(is_array($data['lists'])){
            $Author = new BookAuthorModel();
            $Cate = new BookCateModel();
            $cates = $Cate->getCateArray();
            foreach ($data['lists'] as $k=>$v){
                if($v['author_id'] > 0){
                    $data['lists'][$k]['author_name'] = $Author->getAuthorName($v['author_id']);
                }
                if($v['cate_id'] > 0){
                    $data['lists'][$k]['cate_name'] = $cates[$v['cate_id']];
                }
            }
        }
        if($this->_useCache){
            S($cacheName,$data);
        }
        return $data;
    }

    /**
     * 根据id获取书本名称
     * @param type $book_id
     * @return boolean
     */
    public function getBookName($book_id){
        if(empty($book_id)){            return FALSE;}
        $name = $this->where("`{$this->pk}` = '{$book_id}'")->field('book_name')->find();
        if(empty($name['book_name'])){
            return FALSE;
        }
        return $name['book_name'];
    }

    /**
     * 检索书籍名称
     * @param type $name
     * @return boolean
     */
    public function searchBookName($name){
        if(empty($name)){            return FALSE;}
        return $this->where("`book_name` = '{$name}'")->select();
    }

    public function getBookId($book_name){
        if(empty($book_name)){            return FALSE;}
        $id = $this->where("`book_name` = '{$book_name}'")->field($this->pk)->find();
        if($id){
            return $id[$this->pk];
        }
        return FALSE;
    }

    //获取推荐书籍
    public function getTjBookOfCate($cate_id){
        if($cate_id > 0){
            $Cate = new BookCateModel();
            $cate = $Cate->where("`cate_id` = '{$cate_id}'")->field('tj_book')->find();
            $ids = $cate['tj_book'];
            if(empty($ids)){
                return FALSE;
            }
            $ids = str_replace(':',',',$ids);
            return $this->where("`$this->pk` IN ({$ids})")->field('book_name,book_id')->select();
        }
    }



}
